package com.itheima.service.impl;

import com.itheima.mapper.OrderDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVo;
import com.itheima.service.OrderService;
import lombok.Data;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sf.format(orderSetting.getOrderDate());
        Date parse = sf.parse(format);
        if (orderDao.count(parse)==0){
            orderDao.addNumberByOrderDate(orderSetting);
        }else {
            orderDao.editNumberByOrderDate(orderSetting,parse);
        }
    }

    @Override
    public List<OrderSettingVo> getOrderSettingByMonth(Date month) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
        String format = sf.format(month);
        List<OrderSetting> orderSettings=orderDao.getOrderSettingByMonth(format);
        List<OrderSettingVo> orderSettingVos=new LinkedList<>();
        for (int i = 0; i < orderSettings.size(); i++) {
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
            String format1 = sf1.format(orderSettings.get(i).getOrderDate());
            String[] sp=format1.split("-");
            //orderSettingVos.get(i).setOrderDate(sp[1]);
            orderSettingVos.add(new OrderSettingVo(sp[2],orderSettings.get(i).getNumber(),orderSettings.get(i).getReservations()));
        }
        return orderSettingVos;
    }

    @Override
    public void download(String filename) throws Exception{
        //在内存中创建一个Excel文件对象
        XSSFWorkbook excel = new XSSFWorkbook();
        //创建Sheet页
        XSSFSheet sheet = excel.createSheet("itcast");

        //在Sheet页中创建行，0表示第1行
        XSSFRow row1 = sheet.createRow(0);
        //创建单元格并在单元格中设置值，单元格编号也是从0开始，1表示第2个单元格
        row1.createCell(0).setCellValue("日期");
        row1.createCell(1).setCellValue("可预约数量");

        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(1).setCellValue("2024-01-01");
        row2.createCell(2).setCellValue("100");

        FileOutputStream out = new FileOutputStream(new File("D:\\"+filename));
        //通过输出流将内存中的Excel文件写入到磁盘上
        excel.write(out);

        //关闭资源
        out.flush();
        out.close();
        excel.close();
    }

    @Override
    public void upload(MultipartFile excelFile) throws Exception{
        InputStream in=excelFile.getInputStream();
        XSSFWorkbook excel = new XSSFWorkbook(in);

        XSSFSheet sheet = excel.getSheetAt(0);
        int last=sheet.getLastRowNum();
        ArrayList<OrderSetting> orderSettings=new ArrayList<>();

        for (int i = 1; i < last; i++) {
            OrderSetting orderSetting=new OrderSetting();
            orderSetting.setOrderDate(sheet.getRow(i).getCell(0).getDateCellValue());
            XSSFCell cell1=sheet.getRow(i).getCell(1);
            cell1.setCellType(CellType.STRING);
            orderSetting.setNumber(Integer.parseInt(cell1.getStringCellValue()));
            orderSettings.add(orderSetting);
        }
        orderDao.insert(orderSettings);
        in.close();
        excel.close();
    }
}
